﻿using System; 
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace TestApplication
{
    public partial class ctlMain : UserControl
    {
        private TestApplication owner;
        Random random = new Random();

        public ctlMain(TestApplication _owner)
        {
            owner = _owner;
            InitializeComponent();
        }

        public void UpdateGpsFields(DateTime time, double lat, double lon)
        {
            this.labelTime.Text = "Time: " + time.ToLongTimeString();
            this.labelLatitude.Text = "Latitude: " + lat.ToString();
            this.labelLongitude.Text = "Longitude: " + lon.ToString();
        }

        public void UpdateCellFields(string formattedString)
        {
            this.labelCellData.Text = formattedString;
        }

        public void UpdateWifiFields(Interfaces.WifiData[] data_list)
        {
            foreach(Interfaces.WifiData data in data_list)
            {
                if (!this.listBoxWifi.Items.Contains(data.Name.ToString()))
                {
                    this.listBoxWifi.Items.Add(data.Name.ToString());
                }
            }
        }

        public void AddDataPair(Interfaces.IAbsoluteData pos, string hash)
        {
            listBoxPairedData.Items.Add(hash + " : " + pos.Latitude + " / " + pos.Longitude);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Interfaces.PositionOriginator[] originators = { Interfaces.PositionOriginator.Cell, Interfaces.PositionOriginator.Wifi };
            
            string[] temp = owner.Host.GetCurrentContext(originators);
            string view = "Current context:\r\n";
            foreach (string s in temp)
            {
                view += s + "\r\n";
            }
            System.Windows.Forms.MessageBox.Show(view);
        }

        private void buttonMarkerTest_Click(object sender, EventArgs e)
        {
            if (tbLat.Text == String.Empty)
            {
                tbLat.Text = random.Next(-85, 85).ToString();
            }
            if (tbLon.Text == String.Empty)
            {
                tbLon.Text = random.Next(-185, 185).ToString();
            }

            try
            {
                if (owner.myTestMarkerId >= 10)
                {
                    owner.Host.MoveMarker(owner.myTestMarkerId, Double.Parse(tbLat.Text), Double.Parse(tbLon.Text));
                }
                else
                {
                    owner.myTestMarkerId = owner.Host.NewMarker(Double.Parse(tbLat.Text), Double.Parse(tbLon.Text), owner.myProtoId, owner.myTestMarkerId.ToString(), null);
                }

            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

            tbLat.Text = String.Empty;
            tbLon.Text = String.Empty;
        }
    }
}
